# -*- coding: utf-8 -*-

'''
/**
 * @file        edb_did_define.py
 * @brief       XXXX
 * @note        XXXX
 * @author      靳普诏(puzhao.jin@hopechart.com)
 * @date        2024/12/07
 * @version     1.0

 * @par         修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th> Description
 * <tr><td>2024/12/07   <td>1.0         <td>靳普诏       <td> 创建初始版本
 * @copyright   xxxx
 */
'''

import sys
import os
import argparse

from datetime import datetime
# 获取当前日期
CURR_DATE_STRING = datetime.now().strftime("%Y-%m-%d")
# 获取当前时间
CURR_TIME_STRING = datetime.now().strftime("%H:%M:%S")
## 获取当前脚本所在的目录
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# 输出目录路径
# print("SCRIPT_DIR: "+ SCRIPT_DIR)


sys.path.append("D:/AZ_Folder/script-tools/pyazlib")
from AzModule3 import *


if __name__ == "__main__":


    ## 获取脚本传参
    argv = sys.argv
    argc = len(argv)
    # 打印命令行参数
    #for i in range(1, argc):
        #print("参数", i, ":", argv[i])


    # 设置命令行参数解析器
    # parser = argparse.ArgumentParser(description='Filter log file based on keywords.')
    # parser.add_argument('input_file', type=str, help='Input log file path')
    # parser.add_argument('output_file', type=str, help='Output filtered log file path')
    # parser.add_argument('filter_list', nargs='+', type=str, help='List of keywords to filter')

    # 解析命令行参数
    # args = parser.parse_args()

        
    print("============= BIGEN: edb_did_define.py =============")

    j2 = TAzModule3(f"{SCRIPT_DIR}/edb_did_define.xlsx", f"{SCRIPT_DIR}/ref")

    # 读取xlsx文件
    j2.LoadXlsxRowSheet("did_define")

    # 排序did 并 计算VBOffset
    py_edb_did_def_count = 0
    py_ram_buff_size = 0
    voffset = 0

    j2.user["did_define"].sort(key=lambda x: x["DID"])

    for did in j2.user["did_define"]:   # 这是个列表字典 : [{}]
        py_edb_did_def_count += 1
        did["VBOffset"] = voffset
        if did["DType"] in ["Mem", "Str"]:
            voffset += (1 + did["DLen"])
            if did["DLen"] > 255:
                print(f'ERROR: DLen:{did["DLen"]} is too large!')
                sys.exit(1)
        elif did["DType"] in ["Bytes"]:
            voffset += did["DLen"]
            if did["DLen"] > 255:
                print(f'ERROR: DLen:{did["DLen"]} is too large!')
                sys.exit(1)
        elif did["DType"] in ["U08"]:
            voffset += 1
        elif did["DType"] in ["U16"]:
            voffset += 2
        elif did["DType"] in ["U32"]:
            voffset += 4
        elif did["DType"] in ["U64"]:
            voffset += 8
        else:
            print(f'ERROR: DType:{did["DType"]} is not support!')
            sys.exit(1)

    py_ram_buff_size += voffset
    j2.user["py_ram_buff_size"] = py_ram_buff_size
    j2.user["py_edb_did_def_count"] = py_edb_did_def_count

    print(f"@@@@j2.user: \n{j2.user}")

    j2.Render("edb_did_type.inc.j2", f"{SCRIPT_DIR}/out/edb_did_type.inc")
    j2.Render("edb_did_type.h.j2", f"{SCRIPT_DIR}/out/edb_did_type.h")
    j2.Render("edb_cfg.h.j2", f"{SCRIPT_DIR}/out/edb_cfg.h")
    j2.Render("edb_cfg.c.j2", f"{SCRIPT_DIR}/out/edb_cfg.c")

    print("============= END: edb_did_define.py =============")



## Generated on "2024-12-07 17:28:08" by the tool "gen_hq_file.py >> V20231119" 

